<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="/data/resource/js/jquery.js"></script>
    <script src="/packages/resource/pintuer/pintuer.js"></script>
    <link rel="stylesheet" href="/packages/resource/pintuer/pintuer.css">
    <link rel="stylesheet" href="/packages/resource/css/pager.css">
</head>
<body>
<style>
    table td, table th {
        vertical-align: middle;
    }
    body{
        padding: 10px;
    }
</style>
<div class="pintuer">
    <div class="alert alert-blue">
        提示：不符合条件的操作不会生效
        <br>
        1. 你不能将一个分销商指向比他还低的层级
        <br>
        2. 你不能将一个分销商指定为一个不是分销商的下级
        <br>
        3. 普通会员将成为下级买家
    </div>

    <a class="button bg-blue button-small margin-right" href="/admin/?act=proxy_order&op=proxy_sel_member">
        查看所有
    </a>
    <a class="button bg-blue button-small margin-right" href="/admin/?act=proxy_order&op=proxy_sel_member&type=0">
        只查看买家
    </a>

    <a class="button bg-blue button-small margin-right" href="/admin/?act=proxy_order&op=proxy_sel_member&type=2">
        只查看分销商
    </a>

    <table class="table table-bordered">
        <tr class="thead">
            <th>名称</th>
            <th>身份类型</th>
            <th>上级会员</th>
            <th>操作</th>
        </tr>
        <?php
            $m = Model()->table('member');
            $condition = [];
            if (!isset($_GET['type'])) {
                $list = $m->page(10)->select();
            }
            else{
                $m->table('member,proxy')->join('left')->on('member.member_id = proxy.member_id');
                $condition = [];
                if ($_GET['type'] == 0) {
                    $condition['proxy.is_buyer'] = 1;
                }
                else{
                    $condition['proxy.is_buyer'] = 0;
                }
                $list = $m->where($condition)->page(10)->select();
            }
            if ($list) {
                foreach ($list as $item) {
                    $pinfo = ml('proxy')->find($item['member_id']);

                    ?>
                    <tr>
                        <td><?php echo $item['member_name'] ?></td>
                        <td>
                            <?php
                                if ($pinfo && $pinfo['is_buyer'] == 1) {
                                    echo "买家";
                                }
                                else if($pinfo){
                                    $dis = new Distribute($item['member_id']);
                                    echo $dis->getLevel() . '级分销商';
                                }
                                else{
                                    echo "普通会员";
                                }
                            ?>
                        </td>
                        <td>
                            <?php
                                if ($pinfo && $pinfo['parent_id'] > 0) {
                                    $pminfo = ml('member')->find($pinfo['parent_id']);
                                    echo $pminfo['member_name'];
                                }
                            ?>
                        </td>
                        <td>
                            <a href="javascript:;" data-id="<?php echo $item['member_id']?>" class="button bg-blue choose">选择</a>
                        </td>
                    </tr>
                    <?php
                }

            }
        ?>
    </table>
    <div class="_pager margin-big-top">
        <?php echo $m->showpage() ?>
    </div>
</div>
<script src="/data/resource/layer/layer.js"></script>
<script>
    $('.choose').click(function () {
        var id = $(this).attr('data-id');
        alert(id);
        var index = layer.load();
        $.get('/admin/?act=proxy_order&op=give_member&parent='+parent.sel_id+'&id=' + id, function (msg) {
            layer.close(index);
            layer.msg(msg, function () {
                parent.layer.closeAll();
            })
        });
    });
</script>

</body>
</html>